<?php
// 简单的数据库连接测试脚本
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "脚本开始执行\n";

// 尝试几种不同的身份验证组合
$configs = [
    [
        'host' => '127.0.0.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root'
    ],
    [
        'host' => '127.0.0.1',
        'port' => 3306,
        'user' => 'root',
        'pass' => ''
    ],
    [
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'pass' => 'root'
    ],
    [
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'pass' => ''
    ]
];

foreach ($configs as $index => $config) {
    echo "\n==================================================\n";
    echo "测试配置 #" . ($index + 1) . "\n";
    echo "主机: " . $config['host'] . "\n";
    echo "端口: " . $config['port'] . "\n";
    echo "用户: " . $config['user'] . "\n";
    echo "密码: " . str_repeat('*', strlen($config['pass'])) . ($config['pass'] === '' ? '(空密码)' : '') . "\n";
    
    try {
        $dsn = "mysql:host=" . $config['host'] . ";port=" . $config['port'];
        $pdo = new PDO($dsn, $config['user'], $config['pass']);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        echo "✓ 连接成功!\n";
        
        // 获取MySQL版本
        $stmt = $pdo->query('SELECT VERSION()');
        $version = $stmt->fetchColumn();
        echo "MySQL版本: {$version}\n";
        
        // 列出数据库
        $stmt = $pdo->query('SHOW DATABASES');
        $databases = $stmt->fetchAll(PDO::FETCH_COLUMN);
        
        echo "可用数据库:\n";
        foreach ($databases as $db) {
            echo "- {$db}\n";
        }
        
        // 测试创建数据库
        echo "尝试创建数据库 api_mh...\n";
        $pdo->exec("CREATE DATABASE IF NOT EXISTS `api_mh` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
        echo "✓ 数据库创建成功!\n";
        
        break; // 如果成功，跳出循环
    } catch (PDOException $e) {
        echo "✗ 连接失败: " . $e->getMessage() . "\n";
    }
}

echo "\n==================================================\n";
echo "脚本执行完成\n"; 